package com.polaris.live.popup.party

import android.content.Context
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.databinding.PopupInvitingSeatNoticeBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.payload.InviteSiteNoticeExtBean
import com.polaris.live.im.sfs.bean.live.payload.InviteSiteNoticePayloadBean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 *  邀请上座通知
 * @author Created by 天晴 on 2024/2/27 19:18
 * @since 1.0.0
 **/
class InvitingSeatNoticePopup(
    context: Context,
    private val mLiveMessage: SendLiveMessage,
    private val onNo: () -> Unit,
    private val onOk: (site: Int?) -> Unit
) : BottomPopupView(context) {
    private lateinit var mBinding: PopupInvitingSeatNoticeBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_inviting_seat_notice
    }


    override fun onCreate() {
        super.onCreate()
        mBinding = PopupInvitingSeatNoticeBinding.bind(popupImplView)

        val lp = mBinding.bottom.layoutParams as ViewGroup.LayoutParams
        lp.height = NavigationBarUtils.getNavigationBarHeight(context)
        mBinding.bottom.layoutParams = lp

        val user = (mLiveMessage.ext as InviteSiteNoticeExtBean).user
        val payload = (mLiveMessage.payload as InviteSiteNoticePayloadBean)
        user?.let {
            Glide.with(context)
                .load(ImageUtils.imgPathSmall(it.avatar))
                .transform(CircleCrop())
                .into(mBinding.avatarImg)
            mBinding.avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(it.avatarFrame))
            mBinding.name.text = it.nickname
        }


        mBinding.apply {
            okBtn.setOnSingleClickListener {
                dismiss()
                onOk.invoke(payload.site)
            }
            noBtn.setOnSingleClickListener {
                dismiss()
                onNo.invoke()
            }
        }

    }

    companion object {

        fun show(
            context: Context,
            liveMessage: SendLiveMessage,
            no: () -> Unit,
            ok: (site: Int?) -> Unit
        ) {
            XPopup.Builder(context)
                .isViewMode(false)
                .hasNavigationBar(false)
                .navigationBarColor(R.color.white)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(InvitingSeatNoticePopup(context, liveMessage, no, ok))
                .show()
        }
    }
}